package com.ezcoding.service;

import com.ezcoding.dao.CompanyDao;
import com.ezcoding.dao.FinanceDao;
import com.ezcoding.dao.UserDao;
import com.ezcoding.model.Company;
import com.ezcoding.model.FinanceItem;
import com.ezcoding.model.Message;
import com.ezcoding.model.User;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FinanceService {
    private static FinanceService instance = new FinanceService();

    public static FinanceService getInstance() {
        return instance;
    }

    public Map<String, Object> addFinanceItem(
            String companyId,
            int type,
            String category,
            String subject) {
        if (Tools.checkParaIsInValidate(companyId, category, subject)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        if (CompanyDao.dao.getCompanyByCompanyId(companyId) == null) {
            return StatusUtil.getFailedStatusWithMessage("企业不存在！");
        }

        FinanceItem financeItem = new FinanceItem();

        financeItem.setItemId("" + System.currentTimeMillis());
        financeItem.setCompanyId(companyId);
        financeItem.setType(type);
        financeItem.setCategory(category);
        financeItem.setSubject(subject);

        if (!financeItem.save()) {
            return StatusUtil.getFailedStatusWithMessage("添加科目失败！");
        }

        return StatusUtil.getSuccessStatusWithMessage("添加科目成功！");
    }

    public Map<String, Object> getFinanceItems(String companyId) {
        if (Tools.checkParaIsInValidate(companyId)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        List<FinanceItem> financeItems = FinanceDao.dao.getFinanceItems(companyId);

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        data.put("financeItems", financeItems);
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "刷新成功！");
    }

    public Map<String, Object> addFinanceRecord(
            String username,
            String companyId,
            String itemId,
            double money,
            String remark) {
        if (Tools.checkParaIsInValidate(companyId, itemId)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        FinanceItem financeItem = FinanceDao.dao.getFinanceItem(companyId, itemId);
        if (financeItem == null) {
            return StatusUtil.getFailedStatusWithMessage("财务科目不存在！");
        }

        Company company = CompanyDao.dao.getCompanyByCompanyId(companyId);
        if (company == null) {
            return StatusUtil.getFailedStatusWithMessage("企业不存在！");
        }

        if (financeItem.getType() == 0) {
            //收入
            company.setTotalMoney(company.getTotalMoney() + money);
        } else {
            //支出
            company.setTotalMoney(company.getTotalMoney() - money);
        }

        if(!company.update()){
            return StatusUtil.getFailedStatusWithMessage("企业信息更新失败！");
        }

        User user = UserDao.dao.getUserByUsername(username);
        Message message = new Message(
                username,
                "",
                1,
                money,
                0,
                0,
                0,
                Message.MESSAGE_TYPE_FINANCEIN + financeItem.getType(),
                user == null ? "" : user.getNickName(),
                remark.equals("") ? financeItem.getSubject() : financeItem.getSubject() + "[" + remark + "]");

        if (!message.save()) {
            return StatusUtil.getFailedStatusWithMessage("财务记录新建失败！");
        }

        return StatusUtil.getSuccessStatusWithMessage("财务记录新建成功！");
    }

    public Map<String, Object> deleteFinanceItem(String companyId, String itemId) {
        if (Tools.checkParaIsInValidate(companyId, itemId)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        FinanceItem financeItem = FinanceDao.dao.getFinanceItem(companyId, itemId);

        if (financeItem == null) {
            return StatusUtil.getFailedStatusWithMessage("要删除的财务科目不存在！");
        }

        if (!financeItem.delete()) {
            return StatusUtil.getFailedStatusWithMessage("删除失败！");
        }

        return StatusUtil.getSuccessStatusWithMessage("删除成功！");
    }

    public Map<String, Object> updateFinanceItem(
            String companyId,
            String itemId,
            int type,
            String category,
            String subject) {
        if (Tools.checkParaIsInValidate(companyId, itemId, category, subject)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        FinanceItem financeItem = FinanceDao.dao.getFinanceItem(companyId, itemId);

        if (financeItem == null) {
            return StatusUtil.getFailedStatusWithMessage("要修改的财务科目不存在！");
        }

        financeItem.setType(type);
        financeItem.setCategory(category);
        financeItem.setSubject(subject);

        if (!financeItem.update()) {
            return StatusUtil.getFailedStatusWithMessage("修改失败！");
        }

        return StatusUtil.getSuccessStatusWithMessage("修改成功！");
    }

    public Map<String, Object> financeQuery(
            String username,
            int type1,
            int type2,
            String startDate,
            String endDate) {
        if (Tools.checkParaIsInValidate(username, startDate, endDate)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        List<Message> messages;
        if (type2 == 0) {
            messages = FinanceDao.dao.getFinanceMessages(username, type1, startDate, endDate);
        } else {
            int queryType = (type1 + 1) * 5 + type2 - 1;
            messages = FinanceDao.dao.getFinanceMessagesByType(username, queryType, startDate, endDate);
        }

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        Collections.reverse(messages);

        data.put("messages", messages);
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "查询成功！");
    }
}
